import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/login/login_controller.dart';
import 'package:kkhousekeep/page/me/family_member_controller.dart';
import 'package:kkhousekeep/page/me/member_detail.dart';
import 'package:kkhousekeep/widget/custom_button.dart';

import '../../config/constant.dart';
import '../../models/member_list_entity.dart';
import '../../tools/dialog_util.dart';
import '../../widget/custom_app_bar.dart';
import '../../widget/member_item.dart';

class FamilyMember extends StatelessWidget {
  const FamilyMember({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FamilyMemberController>(
      init: FamilyMemberController(),
      builder: (controller) => Scaffold(
        appBar: CustomAppBar("家庭成员"),
        body: Container(
          color: const Color(0xFFf4f5f6),
          child: SafeArea(
            left: false,
            right: false,
            child: Column(
              children: [
                _buildList(controller),
                _buildAddMemberBtn(context, controller),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildList(FamilyMemberController controller) {
    return Obx(
      () => Expanded(
        child: Padding(
          padding: EdgeInsets.only(left: 32.w, right: 32.w),
          child: ListView.separated(
              itemBuilder: (context, index) {
                if (controller.item[index] is MemberListObjList) {
                  return _buildMemberItem(context, controller, index);
                } else if (controller.item[index] is String) {
                  return _buildCategoryItem(context, controller, index);
                }
                return null;
              },
              separatorBuilder: (context, index) => Divider(
                    height: 16.h,
                    thickness: 0,
                    indent: 0,
                    color: Colors.transparent,
                  ),
              itemCount: controller.item.length),
        ),
      ),
    );
  }

  Widget? _buildMemberItem(
      BuildContext context, FamilyMemberController controller, int index) {
    return MemberItem(
      controller.item[index] as MemberListObjList,
      () {
        if (controller.userType == UserType.owner) {
          Get.to(() => const MemberDetail(), arguments: controller.item[index])
              ?.then((value) {
            if (value == Constant.resultOK) {
              DialogUtil.showNetworkSucceed();
              controller.loadMemberList();
            }
          });
        }
      },
      segue: controller.userType == UserType.owner,
    );
  }

  Widget? _buildCategoryItem(
      BuildContext context, FamilyMemberController controller, int index) {
    return Padding(
      padding: EdgeInsets.only(left: 32.w, top: 32.h),
      child: Text(
        controller.item[index] as String,
        style: TextStyle(color: const Color(0xFF333333), fontSize: 32.sp),
      ),
    );
  }

  Widget _buildAddMemberBtn(
      BuildContext context, FamilyMemberController controller) {
    return CustomButton(
        isAsh: false,
        width: 420.w,
        height: 90.h,
        text: "+  添加成员",
        onPressed: () {
          Get.to(() => const MemberDetail())?.then((value) {
            if (value == Constant.resultOK) {
              DialogUtil.showNetworkSucceed();
              controller.loadMemberList();
            }
          });
        }).marginOnly(top: 30.h, bottom: 60.h);
  }
}
